package com.guiwu.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;

/**
 * 图标控制器
 * 处理网站图标请求
 */
@Controller
public class IconController {

    /**
     * 处理favicon.ico请求，返回icon.png
     */
    @GetMapping("/favicon.ico")
    public ResponseEntity<Resource> favicon() throws IOException {
        Resource resource = new ClassPathResource("static/icon.png");
        
        if (resource.exists()) {
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
                    .header(HttpHeaders.CACHE_CONTROL, "public, max-age=31536000") // 缓存1年
                    .body(resource);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 处理icon.png请求
     */
    @GetMapping("/icon.png")
    public ResponseEntity<Resource> icon() throws IOException {
        Resource resource = new ClassPathResource("static/icon.png");
        
        if (resource.exists()) {
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
                    .header(HttpHeaders.CACHE_CONTROL, "public, max-age=31536000") // 缓存1年
                    .body(resource);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
